iT邦幫忙

2023 iThome 鐵人賽

DAY 4
0
自我挑戰組

和地鼠做30天好朋友系列 第 4

地鼠,幫我寫情書給 Alice 吧

  • 分享至 

  • xImage
  •  

Alice 是我的初戀...(威~開玩笑的

今天我們來看看能不能寫情書給 Alice 吧!那讓我們來看看:甚麼是情書

  1. 首先就是要個載體、有張紙...
  2. 然後在紙上面寫可以打動 Alice 的文字...
  3. 至於要不要送出去...就看你囉...(此步驟可以省略 skip ❌)

那地鼠到底在這一塊能幫我們做什麼呢?

  • 建立檔案
  • 寫內容到檔案裡面
package main

import (
	"os"
)

func main() {
	f, err := os.Create("./sample.txt")
	
	if err != nil {
		panic(err)
	}

	f.WriteString("You are the apple of my eye")
}

好吧,我覺得可以請 ChatGPT 幫我寫一下情書...

https://ithelp.ithome.com.tw/upload/images/20230904/20106999wKciabqBke.png

所以程式碼可以改成這樣

package main

import (
	"os"
)

func main() {
	f, err := os.Create("./sample.txt")
	
	if err != nil {
		panic(err)
	}

	f.WriteString(`Dear Alice,

I wanted to take a moment to express the feelings that have been growing inside me. From the moment we met, your smile has lit up my world, and your laughter has been a melody in my heart. I cherish every moment we spend together, and I can't help but think about you constantly. You are the sunshine in my life, and I hope that we can continue to create beautiful memories together.
	
With all my love,
Jimmy`)

golang 建立檔案就是使用os這個 lib,之後用Create即可...
照慣例讓我們看看 Golang 文件怎麼說:https://pkg.go.dev/os#Create

我就不在水這種文字了,你們可以自己去看看...我覺得有時候我們需要學著好好看文件 👈 好啦,我也是說我自己也該學著好好看文件這樣...

啊 panic 其實也是 Golang 的builtin 函數,這個可以導讀一下:https://pkg.go.dev/builtin#panic

func panic ¶
func panic(v any)
The panic built-in function stops normal execution of the current goroutine. When a function F calls panic, normal execution of F stops immediately. Any functions whose execution was deferred by F are run in the usual way, and then F returns to its caller. To the caller G, the invocation of F then behaves like a call to panic, terminating G's execution and running any deferred functions. This continues until all functions in the executing goroutine have stopped, in reverse order. At that point, the program is terminated with a non-zero exit code. This termination sequence is called panicking and can be controlled by the built-in function recover.

Starting in Go 1.21, calling panic with a nil interface value or an untyped nil causes a run-time error (a different panic). The GODEBUG setting panicnil=1 disables the run-time error.

簡單來說他就是可以直接停止當前的goroutine執行,在golang裡面程式執行的單位就是goroutinemain也是一種goroutine...

最後,讓我來講一下在Golang 裡面如果要做文字的多行,可以使用 ` 這個符號
包在 f.WriteString()裡面就好啦! 情書完成!

Alice: 我拒絕
.
.
.
Me:🥺


上一篇
地鼠,查一下ip 吧
下一篇
地鼠,來測試吧
系列文
和地鼠做30天好朋友12
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言